﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AppTemplate.UI.Controls
{
    /// <summary>
    /// Spinner.xaml 的交互逻辑
    /// </summary>
    public partial class Spinner
    {
        public Spinner()
        {
            InitializeComponent();
            this.Loaded += InitLayout;
        }

        private void InitLayout(object sender, RoutedEventArgs e)
        {
            //移除事件处理,否则每次加载进visual tree时会重新触发
            this.Loaded -= InitLayout;

            int dotCount = DotCount;
            double r = Math.PI * Width;
            double size = r / (dotCount + 8);//根据数量自动计算大小

            //计算canvas中心坐标
            double originX = (Width - size) / 2;
            double originY = (Height - size) / 2;
            for (int i = 0; i < dotCount; i++)
            {
                //角度
                var deg = i == 0 ? 0 : 360 / dotCount * i;
                //弧度
                var ra = deg * Math.PI / 180;

                //坐标
                var x = Math.Cos(ra) * originX + originX;
                var y = Math.Sin(ra) * originY + originY;

                //根据数量计算每个点透明度，保证最后一个有0.1的透明度
                var opacity = 1 - (0.9 / dotCount) * i;
                var dot = new Ellipse { Width = size, Height = size, Fill = DotFill, Opacity = opacity };

                Canvas.SetTop(dot, y);
                Canvas.SetLeft(dot, x);

                canvas.Children.Add(dot);
            }


            SetSpinAnimation();
        }

        private void SetSpinAnimation()
        {
            var storyBoard = (Storyboard)this.FindResource("storyBoard");
            canvas.BeginStoryboard(storyBoard);
            storyBoard.Begin();
        }

        /// <summary>
        /// 圆点数量
        /// </summary>
        public int DotCount
        {
            get { return (int)GetValue(DotCountProperty); }
            set { SetValue(DotCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DotCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DotCountProperty =
            DependencyProperty.Register(nameof(DotCount), typeof(int), typeof(Spinner), new PropertyMetadata(10));


        /// <summary>
        /// 圆点填充颜色
        /// </summary>
        public SolidColorBrush DotFill
        {
            get { return (SolidColorBrush)GetValue(DotFillProperty); }
            set { SetValue(DotFillProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DotFill.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DotFillProperty =
            DependencyProperty.Register(nameof(DotFill), typeof(SolidColorBrush), typeof(Spinner), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(130, 130, 130))));


    }
}
